package com.example.lastwork2.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.lastwork2.R;
import com.example.lastwork2.model.Order;

import java.util.List;

public class OrderAdapter extends BaseAdapter {
    //获取上下文环境
    private Context context;
    //获取item布局
    private int layoutId;
    //数据源
    private List<Order> orderList;

    //布局控件
    private TextView orderName;
    private TextView orderTime;
    private TextView orderPrice;






    public OrderAdapter(){}

    public OrderAdapter(Context context, int layoutId, List<Order> orderList) {
        this.context = context;
        this.layoutId = layoutId;
        this.orderList = orderList;
    }

    @Override
    public int getCount() {
        return orderList.size();
    }

    @Override
    public Object getItem(int position) {
        return orderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //获取布局文件对象
        View view = LayoutInflater.from(context).inflate(layoutId,null);
        //1.获取布局文件控件对象
        orderName = view.findViewById(R.id.order_name);
        orderTime = view.findViewById(R.id.order_time);
        orderPrice = view.findViewById(R.id.order_price);
        //2.获取数据源中当前位置的元素
        int num = orderList.size();
        Order order = orderList.get(num-position-1);

        //3.把数据显示到对应控件中去
        orderName.setText("订单"+(num-position));
        orderTime.setText(order.getTime());
        orderPrice.setText("￥"+order.getSumPrice());




        return view;
    }
}
